//
//  XSJEmoticonCollectionViewCell.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/20.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit

class XSJEmoticonCollectionViewCell: UICollectionViewCell {
    
    var emotions : [XSJEmoticon]? {
    
        didSet {
        
            guard let ets = emotions else {
            
                return
                
            }
            
            
            for value in emoticonButtons {
            
                value.hidden = true
                
            }
            
            
            for (i , value) in ets.enumerate() {
            
                let button = emoticonButtons[i]
                button.emoticon = value

//                button.hidden = false
//                if value.type == "0"{
//                
//                    button.setImage(UIImage(named: value.path!), forState: .Normal)
//                    button.setTitle(nil, forState: .Normal)
//
//                    
//                }else{
//                
//                    button.setImage(nil, forState: .Normal)
//                    button.setTitle((value.code! as NSString).emoji(), forState: .Normal)
//
//                    
//                }
                
            }
            
        }
        
    }
    
    private lazy var emoticonButtons : [XSJEmoticonButton] = [XSJEmoticonButton]()
    
    var indexpath : NSIndexPath? {
    
        didSet {
        
            guard let index = indexpath else {
            
                return
                
            }
            messageLabel.text = "当前显示的是第\(index.section)组第\(index.item)行"

        }
        
    }
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.whiteColor(), fontSize: 30)
        return label
    }()
    
    
    private lazy var deleteEmoticonButton : UIButton = {
    
        
        let button = UIButton()
        button.addTarget(self, action: "deleteEmoticonButtonClick", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
      return button
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setUpUI() {
    
//        contentView.backgroundColor = RandomColor()
        
        addChildButton()
        contentView.addSubview(deleteEmoticonButton)

        contentView.addSubview(messageLabel)
//        messageLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(contentView)
//        }
        
    }
    
    private func addChildButton() {
    
    
        for _ in 0..<20 {
        
            let button = XSJEmoticonButton()
//            button.backgroundColor = RandomColor()
            button.addTarget(self, action: "emoticonClick:", forControlEvents: .TouchUpInside)

            button.titleLabel?.font = UIFont.systemFontOfSize(34)

            contentView.addSubview(button)
            emoticonButtons.append(button)
            
        }
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    
        let itemWidth = width / 7
        let itemHeight = height / 3
        for (i, value) in emoticonButtons.enumerate() {
        
        value.size = CGSize(width: itemWidth, height: itemHeight)

            let colIndex = i % 7

            let rowIndex = i / 7

            //  设置x坐标
            value.x = CGFloat(colIndex) * itemWidth
            //  设置y坐标
            value.y = CGFloat(rowIndex) * itemHeight

        
        }
        
        
        deleteEmoticonButton.size = CGSize(width: itemWidth, height: itemHeight)
        
        deleteEmoticonButton.x = width - itemWidth
        deleteEmoticonButton.y = height - itemHeight

    }
    
    
    
    @objc private func deleteEmoticonButtonClick() {
    
    
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelectedDeleteEmoticonButtonNotification, object: nil)
        
        
    }
    
    
    @objc private func emoticonClick(button: XSJEmoticonButton) {
        let emoticon = button.emoticon
        NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedEmoticonNotification, object: emoticon)
        
    }

    
}
